package com.gaattc.controller.mall;

import com.gaattc.model.Result;
import com.gaattc.model.bo.AddOrderBO;
import com.gaattc.model.bo.CartAccountsBO;
import com.gaattc.model.bo.CartListBO;
import com.gaattc.model.bo.SendCommentBO;
import com.gaattc.model.vo.OrdersByStateVO;
import com.gaattc.service.OrderService;
import com.gaattc.service.OrderServiceImpl;
import com.gaattc.utils.HttpUtils;
import com.google.gson.Gson;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/api/mall/order/*")
public class OrderServlet extends HttpServlet {

    private Gson gson = new Gson();

    private OrderService orderService = new OrderServiceImpl();

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String requestURI = request.getRequestURI();
        String action = requestURI.replace("/api/mall/order/", "");
        if("addOrder".equals(action)) {
            addOrder(request, response);
        } else if("settleAccounts".equals(action)) {
            settleAccounts(request, response);
        } else if("sendComment".equals(action)) {
            sendComment(request, response);
        }
    }

    /**
     * 提交评价
     * @param request req
     * @param response resp
     * @throws IOException io
     */
    private void sendComment(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String requestBody = HttpUtils.getRequestBody(request);
        SendCommentBO sendCommentBO = gson.fromJson(requestBody, SendCommentBO.class);
        orderService.sendComment(sendCommentBO);
        response.getWriter().println(gson.toJson(Result.ok()));
    }

    /**
     * 结算订单
     * @param request req
     * @param response resp
     * @throws IOException io
     */
    private void settleAccounts(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String requestBody = HttpUtils.getRequestBody(request);
        CartListBO cartList = gson.fromJson(requestBody, CartListBO.class);
        int code = orderService.settleAccounts(cartList.getCartList());
        if(code == 0) {
            response.getWriter().println(gson.toJson(Result.ok()));
            return;
        }
        response.getWriter().println(gson.toJson(Result.error("结算失败，请刷新重试")));
    }

    /**
     * 添加订单
     * @param request req
     * @param response resp
     * @throws IOException io
     */
    private void addOrder(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String requestBody = HttpUtils.getRequestBody(request);
        AddOrderBO addOrderBO =gson.fromJson(requestBody, AddOrderBO.class);
        orderService.addOrder(addOrderBO);
        response.getWriter().println(gson.toJson(Result.ok()));
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String requestURI = request.getRequestURI();
        String action = requestURI.replace("/api/mall/order/", "");
        if("getOrderByState".equals(action)) {
            getOrderByState(request, response);
        } else if("deleteOrder".equals(action)) {
            deleteOrder(request, response);
        } else if("confirmReceive".equals(action)) {
            confirmReceive(request, response);
        }
    }

    private void confirmReceive(HttpServletRequest request, HttpServletResponse response) throws IOException {
        int id = Integer.parseInt(request.getParameter("id"));
        orderService.confirmReceive(id);
        response.getWriter().println(gson.toJson(Result.ok()));
    }

    private void deleteOrder(HttpServletRequest request, HttpServletResponse response) throws IOException {
        int id = Integer.parseInt(request.getParameter("id"));
        int code = orderService.deleteOrder(id);
        if(code == 0){
            response.getWriter().println(gson.toJson(Result.ok()));
            return;
        }
        response.getWriter().println(gson.toJson(Result.error("删除失败，订单可能没了哦")));
    }

    private void getOrderByState(HttpServletRequest request, HttpServletResponse response) {
        int state = Integer.parseInt(request.getParameter("state"));
        String token = request.getParameter("token");
        List<OrdersByStateVO> orders = orderService.getOrderByState(state, token);
        try {
            response.getWriter().println(gson.toJson(Result.ok(orders)));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
